package com.example.test.advice.第五章_数组和集合.建议72_生成子列表后不要操作原列表;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Client {

    public static void main(String[] args) {

        getSize();
        //getSize2();
    }

    //subList.size抛异常
    private static void getSize(){
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        List<String> subList = list.subList(0,2);
        //原字符串增加一个元素
        list.add("D");
        System.out.println("原列表长度："+list.size());
        System.out.println("子列表长度："+subList.size());
    }


    private static void getSize2(){
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        List<String> subList = list.subList(0,2);
        list = Collections.unmodifiableList(list);//锁定原列表不可修改
        //原字符串增加一个元素
        list.add("D");
        System.out.println("原列表长度："+list.size());
        System.out.println("子列表长度："+subList.size());
    }
}
